/**
 * 实现一个 HardMan:
 * 
    HardMan("jack") 
    
    输出:
    I am jack
    

    HardMan("jack").rest(10).learn("computer") 
    输出:
    I am jack

    // 等待10秒
    Start learning after 10 seconds

    Learning computer
    

    HardMan("jack").restFirst(5).learn("chinese") 
    输出:

    // 等待5秒
    Start learning after 5 seconds

    I am jack

    Learning chinese

 */
/** 可链式调用
 * hardMan(name)  I am ${name} 
 * 
 * 可延迟，输出 Start learning after ${delay} seconds
 * reset  先执行之前的调用，再延迟，再输出 Start learning after 10 seconds
 * restFirst 先延迟，结束后再输出延迟函数中的内容，再执行之前的调用
 * 
 * learn  输出  Learning ${subject}
 * */ 

function HardMan(name) {
   this.name = name
}
HardMan.prototype.print = function () {
    console.log(`I am ${this.name}`)
    return this
}
HardMan.prototype.rest = function (delay = 0){
    setTimeout(() => {
        console.log(`Start learning after ${delay} seconds`)
    }, delay);

    return this
}


HardMan.prototype.restFirst = function (delay = 0){
    // 阻止之前的函数，或者将这个函数的优先级排最高
    setTimeout(() => {
        this.print()
    },delay)
    return this
}

HardMan.prototype.learn = function (params) {
    console.log(`Learning ${params}`)
    return this
}

let a1 = new HardMan('Jack')
a1.print()
a1.rest(10).learn('computer')
a1.restFirst(5).learn("chinese")

// Class实现
class HardMan {
    constructor(name) {
      this.name = name;
    }
  
    print() {
      console.log(`I am ${this.name}`);
      return this;
    }
  
    rest(seconds) {
      setTimeout(() => {
        console.log(`Start learning after ${seconds} seconds`);
      }, seconds * 1000);
      return this;
    }
  
    restFirst(seconds) {
      setTimeout(() => {
        this.print();
      }, seconds * 1000);
      return this;
    }
  
    learn(subject) {
      console.log(`Learning ${subject}`);
      return this;
    }
  }
  
  new HardMan("jack").print();
  new HardMan("jack").rest(10).learn("computer");
  new HardMan("jack").restFirst(5).learn("chinese");
  